{% import "utils.html" as utils %}
<form method="POST" action="/task/{{ '%s/edit' % task.id if task else 'new' }}">
  <h2 class="text-center">私有签到框架<sup>20211217</sup></h2>
  <hr>
  {% if task.disabled %}
  <p class="alert alert-danger">任务处于禁用状态，使用 <a data-load-method="POST" class="modal_load alert-link" href="/task/{{ task.id }}/run">立即执行</a> 成功签到一次，即可解除。</p>
  {% endif %}

  <div id="run-result"></div>

  <div class="form-group">
    <label class="control-label" for="site-select">
      <a id="siteurl" href="" target="_blank" style="color:black;">网站</a>
    </label>
    <select class="form-control" name="_binux_tplid" id="tplid">
      {% for tpl in tpls %}
      <option value="{{ tpl.id }}" {% if tpl.id == tplid %}selected{% endif %} {% if not tpl.id %}disabled{% endif %}>{{ tpl.sitename or '未命名' }}</option>
      {% endfor %}
    </select>
  </div>

  <div id="variables">
    {% include "task_new_var.html" %}
  </div>

  {% if current_user %}
  <div class="form-group">
    <label class="control-label" for="note">备注</label>
  <!-- MY页面-添加签到-增加提示语 -->
    <input type="text" class="form-control" name="_binux_note" value="{{ task.note or '' }}" id="note" placeholder="尽可能填写15个字符以内">
    {%if task.note %}
      前值： <div class="autowrap showbut" id="pvar_note" >{{ task.note }}
        <div class="btn hljs-button" data-clipboard-target="#pvar_note" style="vertical-align:top">复制</div>
      </div>
    {% endif %}
    <!-- MY页面-添加签到-增加备注图标 -->
  <span class="glyphicon glyphicon-file form-control-feedback" aria-hidden="true"></span>

  </div>
  {% endif %}

  {% if current_user %}
  <div class="form-group">
    <label class="control-label" for="proxy">代理</label>
    <!-- MY页面-添加签到-增加提示语 -->
    <input type="text" class="form-control" name="_binux_proxy" value="{{ proxy }}" id="proxy" placeholder="http://127.0.0.1:8080" pattern="((https?|socks5h?)+://)?([^:@/]+(:[^@/]+)?@)?[^:@/]+:\d+">
    {% if proxy %}
    <div class="text">前值: </div><div class="autowrap showbut" id="pvar_proxy" >{{ proxy }}
      <div class="btn hljs-button" data-clipboard-target="#pvar_proxy" style="vertical-align:top">复制</div>
    </div>
    {% endif %}
    <!-- MY页面-添加签到-增加备注图标 -->
    <span class="glyphicon glyphicon-file form-control-feedback" aria-hidden="true"></span>
  </div>

  <div class="form-group">
    <label class="control-label" for="retry_count">重试次数</label>
    <input type="number" class="form-control" name="_binux_retry_count" list="retry_count" value="{{ retry_count or 8 }}" min="-1" max="8" placeholder="选择或填写重试次数, 不填保持默认" style="width:100%">
    <datalist id="retry_count" style="display:none;">
      <option selected="selected" value="">请选择</option>
      <option value="-1">始终重试</option>
      <option value="0">不重试</option>
      <option value="8">默认</option>
    </datalist>
    {%if retry_count != '' %}
    <div class="text">前值: </div><div class="autowrap showbut" id="pvar_retry_count">{{ retry_count }}
      <div class="btn hljs-button" data-clipboard-target="#pvar_retry_count" style="vertical-align:top">复制</div>
    </div>
    {% endif %}
  </div>

  <div class="form-group">
    <label class="control-label" for="retry_interval">重试间隔</label>
    <input type="number" name="_binux_retry_interval" list="retry_interval" value="{{ retry_interval }}" min="60" placeholder="选择或填写重试间隔, 不填保持默认, 单位: 秒" style="width:100%">
    <datalist id="retry_interval" style="display:none;">
      <option selected="selected" value="">请选择</option>
      <option value="60">每1分钟</option>
      <option value="1800">每30分钟</option>
      <option value="2700">每45分钟</option>
      <option value="3000">每50分钟</option>
      <option value="3600">每小时</option>
      <option value="7200">每2小时</option>
      <option value="43200">每12小时</option>
      <option value="82800">每23小时</option>
      <option value="86400">每天</option>
      <option value="604800">每周</option>
      <option value="2628000">每月</option>
      <option value="7884000">每季</option>
      <option value="31536000">每年</option>
      <option value="" disabled>最短1分钟!</option>
    </datalist>
    {%if retry_interval %}
    <div class="text">前值: </div><div class="autowrap showbut" id="pvar_retry_interval">{{ retry_interval }}
      <div class="btn hljs-button" data-clipboard-target="#pvar_retry_interval" style="vertical-align:top">复制</div>
    </div>
    {% endif %}
  </div>

  {% endif %}
  
  {% if not task and current_user %}
  <div class="checkbox">
    <label>
      <input type="checkbox" name="_binux_tested" id="tested" value="1"> 我今天已经签过到了
    </label>
  </div>
  <label class="control-label" >设置任务分组</label>
  <div class="use_redpackets_content" id="oneCheck">
    {% for group in _groups %}
    <div class="checkbox">
      <label>
        <input type="checkbox" name="group-select-{{ group.encode('utf-8') }}"  {% if group == groupNow %}  checked="" {% endif %}> {{ group }} 
      </label>
    </div>
    {% endfor %}
  </div>
  <div class="form-group">
    <label class="control-label" for="note">设置新分组</label>
	<!-- MY页面-添加签到-增加提示语 -->
    <input type="text" class="form-control" name="New_group" value="" id="New_group" placeholder="尽可能填写15个字符以内">
	<!-- MY页面-添加签到-增加备注图标 -->
	  <span class="glyphicon glyphicon-file form-control-feedback" aria-hidden="true"></span>
  </div>
  {% endif %}



  <div class="text-right">
    {% if task %}
    <a class="btn btn-default" data-method="POST" href="/task/{{ task.id }}/del" data-confirm="是否要删除任务?">删除</a>
    <a class="btn btn-default" data-method="POST" href="/task/{{ task.id }}/disable" data-confirm="是否禁用任务?">禁用</a>
    {% else %}
    <button id="run" data-loading-text="loading..." class="btn btn-default">测试</button>
    {% endif %}
    {% if current_user %}
    <button type="submit" data-loading-text="loading..." class="btn btn-primary">提交</button>
    {% endif %}
  </div>
  <script>
    $(function() {
      // run test
      $('#run').on('click', function() {
        var tplid = $('select[name=_binux_tplid]').val();
        var env = {};
        for (var _env=$('form').serializeArray(), i=_env.length-1; i>=0; i--) {
          env[_env[i].name] = _env[i].value;
        }
        var data = {
          env: JSON.stringify(env),
          _binux_tplid: tplid,
        }

        var $this = $(this);
        $this.button('loading');
        $.ajax('/tpl/run', {
          type: 'POST',
          data: data,
        })
        .done(function(data) {
          $('#run-result').html(data).show()
        })
        .fail(function(jxhr) {
          $('#run-result').html('<h1 class="alert alert-danger text-center">签到失败</h1><div class="well"></div>').show().find('div').text(jxhr.responseText);
        })
        .always(function() {
          $this.button('reset');
        });

        return false;
      });
    })
  </script>

  <script type="text/javascript">
    $(function () {
                var fanxiBox = $("#oneCheck input:checkbox");
                fanxiBox.click(function () {
                  if(this.checked || this.checked=='checked'){
                      fanxiBox.removeAttr("checked");
                      $(this).prop("checked", true);
                    }
                });
            });   
  </script>

  {{ utils.task_new_scripts() }}
  {{ utils.submit_loading() }}
  {{ utils.form_select2(id="#tplid") }}
</form>
